var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:'',
    logo:{},
    info:{},
    icons:[],
    free:false,
    can_play:false,
    can_download:false,
    can_control:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var IsDebug=wx.getStorageSync('IsDebug');
    this.setData({IsDebug:IsDebug});
    var userInfo=wx.getStorageSync('userInfo');
    if(userInfo!="")
    {
      this.setData({userInfo:userInfo});
      if(userInfo.ST==1||userInfo.ST==2||userInfo.RoleID>0)
      {
        this.setData({free:true});
      }
    }
    else
    {
      //wx.navigateTo({url:'/pages/auth/mobile/mobile'})
    }

    if(options.id)
    {
      this.setData({id:options.id});
      this.getinfo();
    }
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var IsDebug=wx.getStorageSync('IsDebug');
    if(IsDebug==0)
    {
      //this.videoContext = wx.createVideoContext('myVideo');
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var IsDebug=this.data.IsDebug;
    if(IsDebug==0)
    {
      //this.videoContext = wx.createVideoContext('myVideo', this);
      //this.videoContext.play();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var IsDebug=this.data.IsDebug;
    if(IsDebug==0)
    {
      this.videoContext = wx.createVideoContext('myVideo', this);
      this.videoContext.pause();
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload:function () {
    console.log("页面销毁");
    var IsDebug=this.data.IsDebug;
    if(IsDebug==0)
    {
      this.videoContext = wx.createVideoContext('myVideo', this);
      this.videoContext.pause();
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getinfo:function(){
    var that=this;
    var id = that.data.id;
    var userInfo=wx.getStorageSync('userInfo');
    var uid=userInfo.Id;
    util.request(api.GetVideoInfo, { Id: id,UserID:uid},'GET').then(function (res) 
    {
      if (res.Status === 100) 
      {
        if(res.Data.info==null)
        {
          wx.showModal({
            title: '提示',
            content:'视频不存在,无法播放',
            success(res1) 
            {
               wx.navigateBack();
            }
          })
          return;
        }

        that.setData({info:res.Data.info});
        that.setData({log:res.Data.log});
        var arr=new Array();
        res.Data.log.UserList.map(function(item){
            if(item.UserIcon!=null)
            {
              arr.push(item.UserIcon)
            }
        });
        if(arr.length<4)
        {
          var left=4-arr.length;
          for(var i=1;i<=left;i++)
          {
            arr.push({"UserIcon":"http://api.whtxqp.com/tx/"+i.toString()+".jpg"}); 
          }
        }
        that.setData({icons:arr});
        
        var RoleID=userInfo.RoleID;
        //非会员 
        if(RoleID===0)
        {
          //会员视频
          if(res.Data.info.SortID===20)
          {
            var FileLength=res.Data.info.FileLength*1000;
            setTimeout(() => {
              that.videoContext = wx.createVideoContext('myVideo');
              that.videoContext.pause();
              wx.showModal({
                title: '提示',
                content:'普通用户只能试播'+res.Data.info.FileLength+'秒,充值会员后可继续播放',
                success(res1) 
                {
                  if (res1.confirm) 
                  {
                    wx.setStorageSync('back_page', 'video');
                    wx.setStorageSync('back_id',that.data.id);
                     wx.navigateTo({url: '/pages/auth/note/index'});
                  }
                  else if (res1.cancel) 
                  {
                    wx.switchTab({ url: '/pages/index/index' });
                  }
                }
              })
            }, FileLength);
          }
          else
          {
              
          }
        }
        else
        {
          that.setData({can_download:true});
          that.setData({can_control:true});
        }
      }
      else
      {
        wx.showModal({
          title: '提示',
          content:'视频不存在,无法播放',
          success(res1) 
          {
             wx.navigateBack();
          }
        })
      }
    });
  },
  set_st1:function(){
     var that=this;
     var id=that.data.id;
     var userInfo=wx.getStorageSync('userInfo');
     var uid=userInfo.Id;

     var log=that.data.log;
     var st=log.ST1;
     if(st==0)
     {
      st=1;
      log.Count1=log.Count1+1;
     }
     else
     {
      st=0;
      log.Count1=log.Count1-1;
     }
     log.ST1=st;
     that.setData({log:log});
     var pd={Fid:id,UserID:uid,ST1:st,ST2:-1,ST3:-1};
     util.request(api.UpdateVideoStatu,pd,'GET').then(function (res) {
      if (res.Status === 100) 
      {
         console.log(res.Msg);
      }
    });
  },
  set_st2:function(){
    var that=this;
    var id=that.data.id;
    var userInfo=wx.getStorageSync('userInfo');
    var uid=userInfo.Id;

    var log=that.data.log;
    var st=log.ST2;
    if(st==0)
    {
     st=1;
     log.Count2=log.Count2+1;
    }
    else
    {
     st=0;
     log.Count2=log.Count2-1;
    }
    log.ST2=st;
    that.setData({log:log});
    var pd={Fid:id,UserID:uid,ST1:-1,ST2:st,ST3:-1};
    util.request(api.UpdateVideoStatu,pd,'GET').then(function (res) {
     if (res.Status === 100) 
     {
        console.log(res.Msg);
     }
   });
 },
 set_st3:function(){
    var that=this;
    var id=that.data.id;
    var userInfo=wx.getStorageSync('userInfo');
    var uid=userInfo.Id;

    var log=that.data.log;
    var st=log.ST3;
    if(st==0)
    {
    st=1;
    log.Count3=log.Count3+1;
    }
    else
    {
    st=0;
    log.Count3=log.Count3-1;
    }
    log.ST3=st;
    that.setData({log:log});
    var pd={Fid:id,UserID:uid,ST1:-1,ST2:-1,ST3:st};
    util.request(api.UpdateVideoStatu,pd,'GET').then(function (res) {
      if (res.Status === 100) 
      {
          console.log(res.Msg);
      }
    });
  },
  downloadFile: function() 
  {
    var info=this.data.info;
    var userInfo=this.data.userInfo;
    if(userInfo.RoleID>0)
    {
      util.getVideoStting(info.FileUrl).then(()=>{
        wx.showToast({title: '下载成功'})
  
      }).catch(()=>{
        wx.showToast({title: '下载失败'})
      })
    }
    else
    {
      wx.showModal({content: '非会员不能免费下载'});
    }
  },

})